package com.tree.backend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tree.backend.common.Result;
import com.tree.backend.entity.dto.PowerEquipmentDTO;
import com.tree.backend.entity.pojo.PowerEquipment;
import com.tree.backend.entity.pojo.PowerZone;
import com.tree.backend.entity.vo.PowerEquipmentVO;
import com.tree.backend.mapper.PowerEquipmentMapper;
import com.tree.backend.mapper.PowerZoneMapper;
import com.tree.backend.service.IPowerEquipmentService;
import com.tree.backend.utils.DataMappingUtils;
import com.tree.backend.utils.UserContext;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 客户基本信息表 服务实现类
 * </p>
 *
 * @author tree
 * @since 2025-11-09
 */
@Service
@RequiredArgsConstructor
public class PowerEquipmentServiceImpl extends ServiceImpl<PowerEquipmentMapper, PowerEquipment> implements IPowerEquipmentService {

    private final PowerZoneMapper powerZoneMapper;

    @Override
    public Result<Page<PowerEquipmentVO>> getList(PowerEquipmentDTO powerEquipmentDTO) {
        // 1. 创建权限映射map
        final Map<Integer, String> powerZoneNameMap = new HashMap<>();
        
        // 2. 条件查询分页
        Page<PowerEquipment> powerEquipmentPage = new Page<>(powerEquipmentDTO.getPage(), powerEquipmentDTO.getPageSize());
        LambdaQueryWrapper<PowerEquipment> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        
        // 使用通用权限过滤方法
        DataMappingUtils.applyPermissionFilter(
                // 权限条件：公司ID不为0
                () -> UserContext.getCompanyId() != 0,
                // 获取权限数据：根据公司ID获取权限区域
                () -> powerZoneMapper.selectList(
                        new LambdaQueryWrapper<PowerZone>()
                                .eq(PowerZone::getCompanyId, UserContext.getCompanyId())),
                // 提取权限ID：区域ID
                PowerZone::getId,
                // 提取权限名称：区域名称
                PowerZone::getZoneName,
                // 权限映射Map
                powerZoneNameMap,
                // 查询条件构建器
                lambdaQueryWrapper,
                // 实体权限字段：设备的区域ID
                PowerEquipment::getPowerZoneId
        );

        // 添加其他查询条件
        lambdaQueryWrapper.like(StringUtils.isNotBlank(powerEquipmentDTO.getEquipmentName()), PowerEquipment::getEquipmentName, powerEquipmentDTO.getEquipmentName())
                .like(StringUtils.isNotBlank(powerEquipmentDTO.getEquipmentCode()), PowerEquipment::getEquipmentCode, powerEquipmentDTO.getEquipmentCode())
                .eq(powerEquipmentDTO.getEquipmentType() != null, PowerEquipment::getEquipmentType, powerEquipmentDTO.getEquipmentType())
                .like(StringUtils.isNotBlank(powerEquipmentDTO.getLocation()), PowerEquipment::getLocation, powerEquipmentDTO.getLocation())
                .eq(powerEquipmentDTO.getStatus() != null, PowerEquipment::getStatus, powerEquipmentDTO.getStatus())
                .ge(powerEquipmentDTO.getRatedPowerMin() != null, PowerEquipment::getRatedPower, powerEquipmentDTO.getRatedPowerMin())
                .le(powerEquipmentDTO.getRatedPowerMax() != null, PowerEquipment::getRatedPower, powerEquipmentDTO.getRatedPowerMax());

        Page<PowerEquipment> page = page(powerEquipmentPage, lambdaQueryWrapper);
        if (page.getRecords().isEmpty()) {
            return Result.success(new Page<>());
        }

        // 使用通用工具类填充区域名称映射
        DataMappingUtils.buildIdNameMap(
                page.getRecords(),
                PowerEquipment::getPowerZoneId,
                powerZoneMapper::selectByIds,
                PowerZone::getId,
                PowerZone::getZoneName,
                powerZoneNameMap
        );

        // 使用增强版通用工具类转换并填充区域名称
        // 注意：这里展示了基本用法，如需多外键映射，可以多次调用并传递powerEquipmentVOList作为existingVOs参数
        List<PowerEquipmentVO> powerEquipmentVOList = DataMappingUtils.convertAndFillNames(
                page.getRecords(),
                PowerEquipmentVO.class,
                PowerEquipment::getPowerZoneId,
                powerZoneNameMap,
                PowerEquipmentVO::setPowerZoneName,
                null  // 首次调用时不需要提供existingVOs
        );

        Page<PowerEquipmentVO> powerEquipmentVOPage = new Page<>();
        BeanUtils.copyProperties(page, powerEquipmentVOPage);
        powerEquipmentVOPage.setRecords(powerEquipmentVOList);
        return Result.success(powerEquipmentVOPage);
    }

}
